package ua.kharkov.knure.dereza.summarytask4.command;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.dereza.summarytask4.core.Constants;
import ua.kharkov.knure.dereza.summarytask4.core.PasswordEncryptor;
import ua.kharkov.knure.dereza.summarytask4.db.dao.*;
import ua.kharkov.knure.dereza.summarytask4.db.dao.mysql.MysqlDAOFactory;
import ua.kharkov.knure.dereza.summarytask4.db.dto.ClientDTO;
import ua.kharkov.knure.dereza.summarytask4.db.dto.LocaleDTO;
import ua.kharkov.knure.dereza.summarytask4.db.dto.RoleDTO;
import ua.kharkov.knure.dereza.summarytask4.db.bean.ClientBean;

/**
 * Login command
 * 
 * @author dereza
 *
 */
public class LoginCommand extends Command{

	private static final long serialVersionUID = 3841261207066969536L;

	Logger log = Logger.getLogger(LoginCommand.class);
	
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse res) {
		log.debug("Command started");
		
		// creates new session if it is necessary
		HttpSession session = req.getSession(true);
		
		// creates the required DAO generator
		MysqlDAOFactory mysqlFactory = (MysqlDAOFactory)DAOFactory.getDAOFactory(DAOFactory.MYSQL);
		
		// creates DAO for client
		ClientDAO clientDAO = mysqlFactory.getClientDAO();
		String login = req.getParameter("login");
		String password = req.getParameter("password");
		
		// gets md5-hash code of password
		PasswordEncryptor encryptor = new PasswordEncryptor();
		password = encryptor.encode(password);
		
		// searches client in db with given login
		log.trace("Request parameter: login --> " + login);
		ClientDTO clientDTO = clientDAO.findClientByLogin(login);
		log.trace("Found client in db --> " + clientDTO);
		
		// error handler
		String errorMessage = null;		
		String forward = Constants.PAGE_ERROR_PAGE;
		if(clientDTO == null || password == null || !clientDTO.getPassword().equals(password)){
			errorMessage = "Cannot find user with such login/password";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		
		// create DAO for client's role
		RoleDAO roleDAO = mysqlFactory.getRoleDAO();
		
		// finds client's role
		RoleDTO roleDTO = roleDAO.findRoleById(clientDTO.getRole());
		log.trace("Found client's role in db --> " + roleDTO);
		if(roleDTO == null){
			errorMessage = "Cannot find client's role";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		
		// create DAO for client's locale
		LocaleDAO localeDAO = mysqlFactory.getLocaleDAO();
		
		// finds client's locale
		LocaleDTO localeDTO = localeDAO.findLocaleById(clientDTO.getLocale());
		log.trace("Found client's locale in db --> " + localeDTO);
		
		if(localeDTO == null){
			errorMessage = "Cannot find client's locale";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		
		// finds all locales and attaches them to session
		List<LocaleDTO> localesList = localeDAO.findAllLocales();
		log.trace("Found locales in db --> " + localesList);
		
		if(localesList == null){
			errorMessage = "Cannot find all locales";
			req.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return forward;
		}
		session.setAttribute("locales", localesList);
		
		// creates client bean
		ClientBean clientBean = new ClientBean();
		clientBean.setId(clientDTO.getId());
		clientBean.setLogin(clientDTO.getLogin());
		clientBean.setName(clientDTO.getName());
		clientBean.setSurName(clientDTO.getSurName());
		clientBean.setMobileNumber(clientDTO.getMobileNumber());
		clientBean.setRole(roleDTO.getName());
		clientBean.setLocale(localeDTO.getName());
		
		if (clientBean.getRole().equals(Constants.ADMIN))
			forward = Constants.COMMAND_ADMIN_LIST_CARDS;
		if (clientBean.getRole().equals(Constants.CLIENT))
			forward = Constants.COMMAND_CLIENT_LIST_CARDS;
		
		// puts clientbean in session
		session.setAttribute("clientbean", clientBean);
		log.info("Logged in new client --> " + login);
		log.debug("ClientBean --> " + clientBean);
		log.debug("Command finished");
		return forward;
	}
	
	@Override
	public String toString() {
		return "LoginCommand";
	}
}